﻿Imports Mysoft.Map.Data
Imports System.Data
Imports System.Data.SqlClient
Imports Mysoft.PubProject.Business

Partial Public Class SelectWorkBatch_XMLHTTP
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim strYwType As String                 '业务类型
        Dim strReturn As String = ""            '返回值



        strYwType = Request.QueryString("ywtype")

        Select Case strYwType.ToUpper
            Case "RelatingWork".ToUpper                                '关联工作项
                strReturn = RelatingWork(getPostStr())
                Exit Select
            Case "CancelRelatingWork".ToUpper                           '取消关联工作项
                strReturn = CancelRelatingWork(getPostStr())
                Exit Select
        End Select

        MyBase.Response.ContentType = "text/HTML"
        MyBase.Response.Clear()
        MyBase.Response.Write(strReturn)      ' 返回操作成功信息
        MyBase.Response.End()
    End Sub

    '功能：关联工作项
    Function RelatingWork(ByVal oids As String) As String
        Dim strSQL As String = ""
        Dim arrOids As String()
        Dim i As Integer = 0
        Dim dtWork As DataTable
        Dim dateBeginDateJh As DateTime
        Dim dateEndDateJh As DateTime
        Dim strReturn As String
        Dim arrMainWorkGUIDs As ArrayList = New ArrayList()

        If oids.Length = 0 Then
            Return "Empty"
        End If

        arrOids = oids.Split("|")

        If arrOids.Length <> 2 Then
            Return "Error"
        End If

        Dim strXgOids As String() = arrOids(0).Split(",")
        Dim strParentOid As String = arrOids(1)

        '收集这次更新影响到的其他的主项计划工作项
        dtWork = MyDB.GetDataTable("SELECT * FROM jd_work WHERE WorkGUID in ('" & arrOids(0).Replace(",", "','") & "')")
        For i = 0 To dtWork.Rows.Count - 1
            If Not arrMainWorkGUIDs.Contains(dtWork.Rows(i)("xgWorkGUID").ToString()) Then
                If Not dtWork.Rows(i)("xgWorkGUID").ToString() = "" Then
                    arrMainWorkGUIDs.Add(dtWork.Rows(i)("xgWorkGUID").ToString())
                End If
            End If
        Next

        '把当前主项计划工作项的GUID也加到数组中
        arrMainWorkGUIDs.Add(strParentOid)

        '更新专项计划工作项的XgWorkGUID
        For i = 0 To strXgOids.Length - 1
            If strSQL = "" Then
                strSQL &= "UPDATE jd_work SET XgWorkGUID = '" & strParentOid & "' WHERE WorkGUID = '" & strXgOids(i) & "'"
            Else
                strSQL &= ";UPDATE jd_work SET XgWorkGUID = '" & strParentOid & "' WHERE WorkGUID = '" & strXgOids(i) & "'"
            End If
        Next
        If MyDB.ExecSQL(strSQL) > 0 Then
            strReturn = "OK"
        Else
            strReturn = "NO"
        End If

        '更新该主项计划的XgjhBeginDate和XgjhEndDate
        If UpdateMainWorksXgDate(arrMainWorkGUIDs) Then
            strReturn = "OK"
        Else
            strReturn = "NO"
        End If

        Return strReturn
    End Function

    '功能：取消关联工作项
    Function CancelRelatingWork(ByVal oids As String) As String
        Dim strSQL As String = ""
        Dim dtWork As DataTable
        Dim dateEndDateJh As DateTime
        Dim dateBeginDateJh As DateTime
        Dim strReturn As String

        Dim arrParam As String() = oids.Split("|")

        Dim arrOids As String() = arrParam(0).Split(",")
        Dim strWorkGUID As String = arrParam(1)

        Dim i As Integer

        '更新被取消关联的专项计划数据
        For i = 0 To arrOids.Length - 1
            If strSQL = "" Then
                strSQL &= "UPDATE jd_work SET XgWorkGUID = NULL WHERE WorkGUID = '" & arrOids(i) & "'"
            Else
                strSQL &= ";UPDATE jd_work SET XgWorkGUID = NULL WHERE WorkGUID = '" & arrOids(i) & "'"
            End If
        Next
        If MyDB.ExecSQL(strSQL) > 0 Then
            strReturn = "OK"
        Else
            strReturn = "NO"
        End If

        '更新该主项计划的XgjhBeginDate和XgjhEndDate
        If UpdateMainWorksXgDate(strWorkGUID) Then
            strReturn = "OK"
        Else
            strReturn = "NO"
        End If

        Return strReturn
    End Function



    '功能：更新主项计划的XgjhBeginDate和XgjhEndDate(多个主项计划工作项GUID)
    '参数：arrMainWorkGUIDs 存放所有需要更新的主项计划的GUID
    Function UpdateMainWorksXgDate(ByVal arrMainWorkGUIDs As ArrayList) As Boolean
        Dim i As Integer
        Dim strSQL As String
        Dim dtWork As DataTable
        Dim dateEndDateJh As String
        Dim dateBeginDateJh As String
        Dim bReturn As Boolean

        For i = 0 To arrMainWorkGUIDs.Count - 1

            dateEndDateJh = ""
            dateBeginDateJh = ""

            '获得与该主项计划关联的专项计划工作项最晚的EndDateJh
            strSQL = "SELECT TOP 1 EndDateJh FROM jd_work WHERE xgWorkGUID = '" & arrMainWorkGUIDs(i) & "' ORDER BY EndDateJh DESC"
            dtWork = MyDB.GetDataTable(strSQL)
            If dtWork.Rows.Count > 0 Then
                If Not Convert.IsDBNull(dtWork.Rows(0)("EndDateJh")) Then
                    dateEndDateJh = dtWork.Rows(0)("EndDateJh").ToString()
                End If
            End If

            '获得与该主项计划关联的专项计划工作项最早的BeginDateJh
            strSQL = "SELECT TOP 1 BeginDateJh FROM jd_work WHERE xgWorkGUID = '" & arrMainWorkGUIDs(i) & "' ORDER BY BeginDateJh"
            dtWork = MyDB.GetDataTable(strSQL)
            If dtWork.Rows.Count > 0 Then
                If Not Convert.IsDBNull(dtWork.Rows(0)("BeginDateJh")) Then
                    dateBeginDateJh = dtWork.Rows(0)("BeginDateJh").ToString()
                End If
            End If

            '更新主项计划的XgjhBeginDate和XgjhEndDate
            If dateBeginDateJh <> "" And dateEndDateJh <> "" Then
                strSQL = "UPDATE jd_work SET XgjhBeginDate = '" & dateBeginDateJh & "',XgjhEndDate = '" & dateEndDateJh & "' WHERE WorkGUID = '" & arrMainWorkGUIDs(i) & "'"
                If MyDB.ExecSQL(strSQL) < 0 Then
                    Return False
                End If
            Else
                strSQL = "UPDATE jd_work SET XgjhBeginDate = NULL ,XgjhEndDate = NULL WHERE WorkGUID = '" & arrMainWorkGUIDs(i) & "'"
                If MyDB.ExecSQL(strSQL) < 0 Then
                    Return False
                End If
            End If
        Next
        Return True
    End Function

    '功能：更新主项计划的XgjhBeginDate和XgjhEndDate(多个单个项计划工作项GUID)
    '参数：strMainWorkGUID 存放需要更新的主项计划的GUID
    Function UpdateMainWorksXgDate(ByVal strMainWorkGUID As String) As Boolean
        Dim arrMainWorkGUIDs As ArrayList = New ArrayList()

        arrMainWorkGUIDs.Add(strMainWorkGUID)

        If UpdateMainWorksXgDate(arrMainWorkGUIDs) Then
            Return True
        Else
            Return False
        End If
    End Function

    Function getPostStr() As String

        Dim b(Request.InputStream.Length - 1) As Byte
        Request.InputStream.Read(b, 0, Request.InputStream.Length)

        Return System.Text.Encoding.UTF8.GetString(b)

    End Function
End Class